package com.mrjoew.weekend.entities.po;

import com.google.common.collect.Sets;
import com.mrjoew.weekend.constants.SymbolConstant;
import com.mrjoew.weekend.entities.BasePO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.SQLDelete;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Christopher Jones
 * @ClassName Client
 * @explain Spring Security Oauth2 Client 配置
 * @date 2020/6/17 0017 20:23
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
@SQLDelete(sql = "UPDATE `sys_security_client` SET deleted = 1 WHERE id = ?")
@Table(name = "sys_security_client")
@Entity
public class SysSecurityClientPO extends BasePO implements ClientDetails {

    private String clientId;

    private String clientSecret;

    private String resourceIds;

    private String scopes;

    private String authorities;

    private Integer accessTokenValiditySeconds;

    @Transient
    private String grantTypes;

    @Override
    public Set<String> getResourceIds() {
        if (StringUtils.isBlank(resourceIds)) {
            return Sets.newHashSet();
        }
        return Sets.newHashSet(resourceIds.split(SymbolConstant.COMMA));
    }

    public Collection<GrantedAuthority> getAuthorities() {
        if (StringUtils.isBlank(authorities)) {
            return Collections.emptyList();
        }
        Set<String> strings = Sets.newHashSet(authorities.split(SymbolConstant.COMMA));
        return strings.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
    }

    @Override
    public boolean isSecretRequired() {
        return false;
    }

    @Override
    public boolean isScoped() {
        return false;
    }

    @Override
    public Set<String> getScope() {
        if (StringUtils.isBlank(scopes)) {
            return Collections.emptySet();
        }
        return Sets.newHashSet(scopes.split(SymbolConstant.COMMA));
    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        if (StringUtils.isBlank(authorities)) {
            return Collections.emptySet();
        }
        return Sets.newHashSet(authorities.split(SymbolConstant.COMMA));
    }

    @Override
    public Set<String> getRegisteredRedirectUri() {
        return null;
    }

    @Override
    public Integer getAccessTokenValiditySeconds() {
        return accessTokenValiditySeconds;
    }

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return null;
    }

    @Override
    public boolean isAutoApprove(String scope) {
        return false;
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        return null;
    }
}
